"""
设备接口定义
Device Interface Definition

定义了所有IoT设备必须实现的基本接口
Defines the basic interface that all IoT devices must implement
"""

from abc import ABC, abstractmethod
from typing import Dict, Any, Optional, List
from pydantic import BaseModel


class DeviceInterface(ABC):
    """
    设备接口抽象类
    Abstract base class for all IoT devices
    
    所有IoT设备都必须继承此接口并实现相应方法
    All IoT devices must inherit from this interface and implement the corresponding methods
    """
    
    def __init__(self, device_id: str):
        """
        初始化设备
        Initialize device
        
        Args:
            device_id (str): 设备唯一标识符 / Device unique identifier
        """
        self.device_id = device_id
        self.device_type = self._get_device_type()
        self.is_online = False
        self.last_heartbeat = None
    
    @abstractmethod
    def _get_device_type(self) -> str:
        """
        获取设备类型
        Get device type
        
        Returns:
            str: 设备类型 / Device type
        """
        pass
    
    @abstractmethod
    async def process_data(self, raw_data: Dict[str, Any]) -> Optional[BaseModel]:
        """
        处理原始数据
        Process raw data
        
        Args:
            raw_data (Dict[str, Any]): 原始数据 / Raw data
            
        Returns:
            Optional[BaseModel]: 处理后的数据模型 / Processed data model
        """
        pass
    
    @abstractmethod
    async def validate_data(self, data: Dict[str, Any]) -> bool:
        """
        验证数据有效性
        Validate data integrity
        
        Args:
            data (Dict[str, Any]): 待验证数据 / Data to validate
            
        Returns:
            bool: 验证结果 / Validation result
        """
        pass
    
    @abstractmethod
    async def send_command(self, command: Dict[str, Any]) -> Dict[str, Any]:
        """
        向设备发送命令
        Send command to device
        
        Args:
            command (Dict[str, Any]): 命令数据 / Command data
            
        Returns:
            Dict[str, Any]: 命令执行结果 / Command execution result
        """
        pass
    
    @abstractmethod
    def get_supported_actions(self) -> List[str]:
        """
        获取设备支持的操作类型
        Get supported action types for the device
        
        Returns:
            List[str]: 支持的操作列表 / List of supported actions
        """
        pass
    
    async def update_online_status(self, is_online: bool) -> None:
        """
        更新设备在线状态
        Update device online status
        
        Args:
            is_online (bool): 在线状态 / Online status
        """
        self.is_online = is_online
        if is_online:
            from datetime import datetime
            self.last_heartbeat = datetime.now()
    
    def get_device_info(self) -> Dict[str, Any]:
        """
        获取设备基本信息
        Get basic device information
        
        Returns:
            Dict[str, Any]: 设备信息 / Device information
        """
        return {
            'device_id': self.device_id,
            'device_type': self.device_type,
            'is_online': self.is_online,
            'last_heartbeat': self.last_heartbeat,
            'supported_actions': self.get_supported_actions()
        }